﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using MINERP.DisjoinProForOutShop.CrystalDataSet;
//using MINERP.DisjoinProForOutShop.CrystalReport;
using MINERP.MODEL.FoundationModel;

using System.IO;
using MINERP.COM;

namespace MINERP.DisjoinProForOutShop.Forms
{
    public partial class FrmPrint : Form
    {
        public FrmPrint(SHOP_Bill_Info insbinfo  )
        {
            InitializeComponent();
            sbinfo = insbinfo;
        }
        SHOP_Bill_Info sbinfo;
        //DSPrintShop dsshop=new DSPrintShop();

        private void FrmPrint_Load(object sender, EventArgs e)
        {
            double PrinterAreas = 0;
            double PrinterCount = 0;
            int PhotoNum = 0;
            int ProductNum = 0;
            double DesginCount = 0;
            double ProductCount = 0;
            double AllCount = 0;
            double OtherCount = 0;
            bool IsNeedPrintMoney = sbinfo.Shop.IsNeedPrintUPrice;
            //DataTable tableTitle = dsshop.tableTitle();
            DataRow titleRow = dsshop.tableTitle.NewRow();
            titleRow["ShopName"] = sbinfo.Shop.ShopName;
            titleRow["Tel1"] = sbinfo.Shop.Tel1;
            titleRow["Tel2"] = sbinfo.Shop.Tel2;
            titleRow["ShopId"] = sbinfo.Shop.ShopId;
            titleRow["shopKeeper"] = sbinfo.Shop.ShopKeeper;
            titleRow["QQ"] = sbinfo.Shop.QQ;
            titleRow["BillId"] = sbinfo.BillId;
            titleRow["BillOrderCode"] = sbinfo.BillOrderCode;
            titleRow["CreateTime"] = sbinfo.CreateTime;
            titleRow["Adress"] = sbinfo.Shop.Address;
            if (IsNeedPrintMoney)
            {
                titleRow["Balance"] = sbinfo.ShopBalance;
                titleRow["BillCount"] = sbinfo.BillCount;
            }
            else
            {
                titleRow["Balance"] = 0;
                titleRow["BillCount"] = 0;
            }
            titleRow["IsNeedPrintMoney"] = IsNeedPrintMoney;


            titleRow["Salesman"] = sbinfo.Salesman;
            titleRow["CreditLevel"] = sbinfo.CreditLevel == null ? 0 : sbinfo.CreditLevel;
            dsshop.tableTitle.Rows.Add(titleRow);

            dsshop.tableTitle.TableName = "tableTitle";


            //DataTable tableDetail = CreateShopBillDetail();
            //DataTable tableSubDetail = CreateShopBillSubDetail();
            List<Bill_Detail_Info> detailList = sbinfo.List_Detail.ToList();

            foreach (Bill_Detail_Info billDetail in detailList)
            {
                ProductNum++;
                DataRow row = dsshop.tableDetail.NewRow();
                row["BillDetailId"] = billDetail.BillDetailId;
                row["OrderInfoCode"] = billDetail.OrderInfoCode;
                row["OrderProductName"] = billDetail.OrderProductName;
                row["CtmName"] = billDetail.CtmName;
                row["Remark"] = billDetail.Remark;
                row["OrderBagName"] = billDetail.OrderBagName;

                dsshop.tableDetail.Rows.Add(row);
                List<Bill_Detail_Detail_Info> subDetail = billDetail.ListDetail_Detail.ToList();
                foreach (Bill_Detail_Detail_Info detail in subDetail)
                {
                    DataRow rowSub = dsshop.tableSubDetail.NewRow();
                    rowSub["DetailName"] = detail.DetailName;
                    rowSub["BillDetailId"] = billDetail.BillDetailId;
                    rowSub["Spec"] = detail.Spec;
                    rowSub["Num"] = detail.Num;
                    rowSub["UnitPrice"] = detail.UnitPrice;
                    if (IsNeedPrintMoney)
                    {
                        rowSub["Count"] = detail.Count;
                    }
                    else
                    {
                        rowSub["Count"] = 0;
                    }

                    rowSub["Area"] = detail.Area;
                    rowSub["Type"] = detail.Type;
                    if (detail.Type == (int)Bill_Detail_Detail_Type.NeedStatArea)
                    {
                        PrinterAreas += detail.Area;
                        PrinterCount += detail.Count;
                        PhotoNum += detail.Num;
                    }
                    else if (detail.Type == (int)Bill_Detail_Detail_Type.UnNeedStatArea)
                    {
                        ProductCount += detail.Count;
                        OtherCount += detail.Count;
                    }
                    else
                    {
                        DesginCount += detail.Count;
                        OtherCount += detail.Count;
                    }
                    AllCount += detail.Count;
                    dsshop.tableSubDetail.Rows.Add(rowSub);
                }
            }
            titleRow["PrinterAreas"] = PrinterAreas;
            titleRow["PrinterCount"] = PrinterCount;
            titleRow["ProductCount"] = ProductCount;
            titleRow["DesginCount"] = DesginCount;
            titleRow["PhotoNum"] = PhotoNum;
            titleRow["ProductNum"] = ProductNum;
            titleRow["AllCount"] = AllCount;
            titleRow["OtherCount"] = OtherCount;
            titleRow["CheckType"] = sbinfo.CheckType;

            dsshop.tableDetail.TableName = "tableDetail";
            dsshop.tableSubDetail.TableName = "tableSubDetail";
            CRPrintShop cr1 = new CRPrintShop();
            cr1.SetDataSource(dsshop);
            //crystalReportViewer1.ReportSource = cr1;
            //crystalReportViewer1.PrintReport();
            cr1.PrintToPrinter(1, true, 0, 0);
            //cr1.Load(Application.StartupPath+"\\"+"report\\CRPrintShop.rpt";
            //cr1.PrintToPrinter();
            
            string excelPath = Application.StartupPath + "\\报表文件\\" + sbinfo.Shop.ShopName;
            if (!Directory.Exists(excelPath))
            {
                Directory.CreateDirectory(excelPath);
            }
            string repPath=excelPath + "\\" + DateTime.Now.ToString().Replace(":", " ") + ".xls";
            cr1.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, repPath);
            Properties.Settings.Default.LastPrintPath = "0|" + repPath;
            Properties.Settings.Default.Save();
            this.Close();
        }

        private void CRPrintShop2_InitReport(object sender, EventArgs e)
        {

        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

        }
    }
}
